﻿(function(w){



w.transformCss = function(node,name,value)
{

	if(!node.transform)
	{
		node.transform={};
	}

	if(arguments.length>2){
		//写
		node.transform[name] = value;
		var returlt = ''
		for(var item in node.transform)
		{
			switch(item)
			{
				case 'rotate':
				case 'skew':
				case 'skewX':
				case 'skewY':
					result= item+'('+ node.transform[item] + 'deg)'
				break;
				case 'scale':
				case 'scaleX':
				case 'scaleY':
					result= item+'('+ node.transform[item] + 'deg)'
				break;
				case 'translate':
				case 'translateX':
				case 'translateY':
					result= item+'('+ node.transform[item] + 'px)'
				break;
			}
		}

		node.style.transform = result
	}
	else
	{

		var value = 0

		if(typeof node.transform[name] == 'undefined')
		{

			if(name == 'scale' || name == 'scaleX' || name == 'scaleY')
			{
				value = 1

			}else{

				value = 0
			}

		}else
		{

			value = node.transform[name]
		}

		return value
	}
}


w.widthCss = function(node,value){


	if(!node.widthVal)
	{
		node.widthVal=0;
	}

	if(arguments.length>1){
		//写
		node.widthVal = value ;
		node.style.width = value + '%';
	}
	else
	{

		var value = 0

		if(typeof node.widthVal == 'undefined')
		{
				value = 0


		}else
		{

			value = node.widthVal
		}

		return value
	}



}


w.dragProgress = function(progress,proressCnl,touchstart,touchmove,touchend){



	progress.addEventListener("touchstart",function(e){

		var touch = e.changedTouches[0]

		var beginValue = (touch.clientX - 40) / $(this).width() *100


		w.widthCss(proressCnl,beginValue)

		// touch.touchstart(beginValue)

		touchstart(beginValue)


	})

	progress.addEventListener("touchmove",function(e){

		var touch = e.changedTouches[0]

		var nowX = (touch.clientX - 40) / $(this).width() * 100;

		w.widthCss(proressCnl,nowX )

		touchmove(nowX)

	})

	progress.addEventListener('touchend',function(e){

		var touch = e.changedTouches[0]

		var nowX = (touch.clientX - 40) / $(this).width() * 100;

		w.widthCss(proressCnl,nowX )

		touchend(nowX)

	})



}

w.drag = function(navs,navList){

	// var navs = document.getElementById("navbar")
	// var navList = document.getElementById("navlist")

	var startX = 0;
	var eleX = 0;

	var beginTime = 0;
	var beginValue = 0;
	var endTime = 0;
	var endValue = 0;
	var disTime = 0;
	var disValue = 0;
	navList.style.transition = '1s'

	navs.addEventListener("touchstart",function(e){

		var touch = e.changedTouches[0]
		startX = touch.clientX
		eleX=w.transformCss(navList,'translateX')
		beginTime  = new Date().getTime()
		beginValue = eleX

	})

	navs.addEventListener("touchmove",function(e){

		var touch = e.changedTouches[0]
		var nowX = touch.clientX;
		var disX = nowX - startX;
		var translateX = disX + eleX

		var minX = document.documentElement.clientWidth - navList.offsetWidth


		if(translateX>0){

			// var scale = 1 - translateX / document.documentElement.clientWidth

			// translateX = translateX * scale

			translateX = 0

		}else if(translateX < document.documentElement.clientWidth - navList.offsetWidth){

			// var over = minX - translateX;

			// var scale = 1 - over / document.documentElement.clientWidth

			// translateX =minX - over * scale

			translateX = minX


		}

		// w.transformCss(navList,'translateX',translateX )

		// endTime = new Date().getTime()

		// endValue = translateX

		// disTime = endTime - beginTime

		// disValue = endValue - beginValue

	})

	// navs.addEventListener('touchend',function(e){

	// 	var touch = event.changedTouches[0]
	// 	var speed = disValue/disTime
	// 	var target = w.transformCss(navList,"translateX") + speed * 100
	// 	var minX = document.documentElement.clientWidth - navList.offsetWidth -15
	// 	var bezier=""
	// 	if(target>0){
	// 		target=0
	// 		bezier="cubic-bezier(.29,1.57,.92,1.67)"
	// 	}else if(target<minX){
	// 		target=minX
	// 		bezier="cubic-bezier(.29,1.57,.92,1.67)"
	// 	}
	// 	navList.style.transition = '1s' + bezier
	// 	w.transformCss(navList,"translateX",target)

	// })

}

})(window)
